using Godot;
using System;

public partial class main : Node
{
	// 交互关系放置在第五个图层
	private PackedScene CreateNewEnemy = GD.Load<PackedScene>("res://scene/enemy.tscn");
	private CharacterBody2D Player;
	private Area2D PlayerArea;
	private CircleShape2D CircleSharp;
	private Camera2D Camera;
	private ShaderMaterial soundMaterial;
	private ShaderMaterial clickMaterial;

	private Vector2 thePosition;
	
	public double Speed = 120.0d;
	public int CheckRange = 10;
	public bool isPlay = false;
	public bool isQuit = false;
	private float VolumeDb;

	public static float Clamp(float value, float min, float max)
	{
		return Math.Min(Math.Max(value, min), max);
	}

	// new enemy.
	public void newEnemy(Vector2 thePosition)
	{	
		// 原来是括号外面有问题......
		CharacterBody2D theEnemy = CreateNewEnemy.Instantiate<CharacterBody2D>();
		theEnemy.Position = thePosition;
		AddChild(theEnemy);
	}

	public override void _Ready()
	{
		Player = GetNode<CharacterBody2D>("Player");
		Camera = GetNode<Camera2D>("Camera2D");
		PlayerArea = GetNode<Area2D>("PlayerArea2D");

		// 测试敌人
		//newEnemy(new Vector2(300f, 500f));

		CircleSharp = GetNode<CollisionShape2D>("PlayerArea2D/CollisionShape2D").Shape as CircleShape2D;
		soundMaterial = GetNode<ColorRect>("Camera2D/Sound").Material as ShaderMaterial;
		clickMaterial = GetNode<ColorRect>("Player/click").Material as ShaderMaterial;
	}

	// Called every frame. 'delta' is the elapsed time since the previous frame.
	public override void _Process(double delta)
	{
		// 移动摄像机
		Camera.Position = Player.Position;

		// 移动玩家
		float newSpeed = (float)(Speed * delta);
		thePosition = new Vector2(
			newSpeed * Input.GetActionRawStrength("ui_right") - 
			newSpeed * Input.GetActionRawStrength("ui_left"), 
			newSpeed * Input.GetActionRawStrength("ui_down") - 
			newSpeed * Input.GetActionRawStrength("ui_up")
		);
		

		// 声音功能
		VolumeDb = Clamp((AudioServer.GetBusPeakVolumeLeftDb(2, 0) + AudioServer.GetBusPeakVolumeRightDb(2, 0) + 300f) / 400f, 0.3f, 0.7f);
		VolumeDb = (VolumeDb - 0.3f) / 0.4f;
		
		if (Input.GetActionRawStrength("ui_click") != 0)
		{
			VolumeDb = 1f;
		}

		// 更新检测范围
		CircleSharp.Radius = 0 == CircleSharp.Radius ? VolumeDb * CheckRange * CheckRange : 0;
		soundMaterial.SetShaderParameter("progress", VolumeDb);
		
		// 检测触发click
		if (VolumeDb > 0.3f)
		{
			clickMaterial.SetShaderParameter("not_click", false);
			funcQuit();
			funcPlay();
			
		}
		else 
		{
			clickMaterial.SetShaderParameter("not_click", true);
		}


		// player移动
		Player.Position += thePosition;
		Player.MoveAndSlide();

		// 定义player角度
		Player.Rotation = - thePosition.AngleTo(new Vector2(0f, -1f));

		// 更新范围判定的位置
		PlayerArea.Position = Player.Position;

	}

	// 进入quit区域
	public void _on_quit_area_entered(Area2D area)
	{
		isQuit = true;
	}

	// 离开quit区域
	public void _on_quit_area_exited(Area2D area)
	{
		isQuit = false;
	}

	// 进入play区域
	public void _on_play_area_entered(Area2D area)
	{
		isPlay = true;
	}
	// 离开play区域
	public void _on_play_area_exited(Area2D area)
	{
		isPlay = false;
	}

	// 开始函数
	public void funcPlay()
	{
		if (isPlay) 
		{
			var scene = ResourceLoader.Load<PackedScene>("res://scene/enemy.tscn");
			GetTree().ChangeSceneToPacked(scene);
			GD.Print("start game");
		}
	}

	// 退出函数
	public void funcQuit()
	{
		if (isQuit)
		{
			GetTree().Quit();	
		}
	}
}



